import type { IActivityListResData, IPrticipantsListResData } from "@/api/activity/types";
import type { WxShareOptions } from "@/components/CustomShare/types";

import { formatToDateTime } from "@/utils/dateUtil";
import { useUserStoreWithOut } from "@/store/modules/user";
import { useActivityWithOut, type IActivityShareCache } from "@/store/modules/activity";
import { navigateTo, reLaunch } from "@/router";
import { ApprovalStatusEnum } from "@/enums/activityStatusEnum";

const imgEmpty = 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/icon/emptyIcon/img-empty.png';
const emptyAvatar = 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/icon/emptyIcon/empty-avatar.png'

// const codeGuideImg =
//   'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/code-guide-img.png';
// const codeGuideImg2 =
//   'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/code-guide-img2.png';


// 获取活动海报配置数据
export function getActivityPosterData(data: IActivityShareCache['activeityData'], codeImg: string): PainterBoard[] {
  const { userInfo } = useUserStoreWithOut()

  const coverUrl = data.coverUrl || imgEmpty

  return [
    {
      css: {
        width: '800rpx',
      },
      views: [
        {
          type: 'view',
          css: {
            background: '#F2F3F5',
            padding: '40rpx'
          },
          views: [
            {
              type: 'view',
              css: {
                marginBottom: '32rpx'
              },
              views: [
                {
                  type: 'image',
                  src: userInfo.avatar || emptyAvatar,
                  css: {
                    width: '96rpx',
                    height: '96rpx',
                    borderRadius: '50%',
                    background: '#fff',
                    objectFit: 'cover'
                  }
                },
                {
                  type: 'text',
                  text: `Hi 我是${userInfo.userName} \n我发现了一个好活动，一起来参加吧~`,
                  css: {
                    color: '#333',
                    fontSize: '28rpx',
                    lineHeight: '44rpx',
                    marginLeft: '16rpx'
                  }
                },
              ]
            },
            {
              type: 'image',
              src: coverUrl,
              css: {
                width: '100%',
                height: '576rpx',
                borderRadius: '16rpx',
                objectFit: 'cover'
              }
            }
          ]
        },
        {
          type: 'view',
          css: {
            padding: '40rpx',
            background: '#fff',
            position: 'relative'
          },
          views: [
            {
              type: 'view',
              css: {
                width: '520rpx',
                color: '#333',
              },
              views: [
                {
                  text: data.title,
                  type: 'text',
                  css: {
                    fontSize: '40rpx',
                    lineClamp: 1,
                    marginBottom: '32rpx',
                    paddingTop: '16rpx'
                  }
                },
                {
                  type: 'view',
                  css: {
                    marginBottom: '8rpx',
                  },
                  views: [
                    {
                      type: 'image',
                      src: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/time-icon-2.png',
                      css: {
                        width: '32rpx',
                        height: '32rpx',
                      }
                    },
                    {
                      type: 'text',
                      text: `${formatToDateTime(data.startTime, 'MM月DD日 HH:mm')} ~ ${formatToDateTime(data.endTime, 'MM月DD日 HH:mm')}`,
                      css: {
                        marginLeft: '16rpx',
                        fontSize: '24rpx',
                        color: '#999996',
                      }
                    }
                  ]
                },
                {
                  type: 'view',
                  css: {
                    marginBottom: '8rpx',
                  },
                  views: [
                    {
                      type: 'image',
                      src: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/address-icon-2.png',
                      css: {
                        width: '32rpx',
                        height: '32rpx',
                      }
                    },
                    {
                      type: 'text',
                      text: data.addressName,
                      css: {
                        width: '85%',
                        marginLeft: '16rpx',
                        fontSize: '24rpx',
                        color: '#999996',
                        lineClamp: 1,
                      }
                    }
                  ]
                }
              ]
            },
            {
              type: 'view',
              css: {
                position: 'absolute',
                right: '40rpx',
                top: '25rpx',
              },
              views: [
                {
                  type: 'image',
                  src: codeImg,
                  css: {
                    width: '160rpx',
                    height: '160rpx',
                    marginBottom: '16rpx'
                  }
                },
                {
                  type: 'text',
                  text: '微信扫一扫',
                  css: {
                    width: '160rpx',
                    display: 'block',
                    fontSize: '28rpx',
                    color: '#999996',
                    textAlign: 'center'
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

/**
 * @description 获取活动分享数据
 * @date 01/08/2024
 * @export
 * @param {IActivityListResData} data
 */
export function getActivityWxShareData(data: IActivityListResData, path: string, personnelList: IPrticipantsListResData[]): WxShareOptions[] {

  const imgSize = {
    width: '420rpx',
    height: '336rpx',
  }

  const coverUrl = data.sharUrl || imgEmpty
  let list: PainterBoardItem[] = []

  personnelList.forEach((item, index) => {
    list.push({
      type: 'image',
      src: item.avatar || emptyAvatar,
      css: {
        width: '44rpx',
        height: '44rpx',
        border: '1px solid #fff',
        borderRadius: '50%',
        marginLeft: index === 0 ? '0' : '-16rpx',
      }
    })
  }
  )

  if (list.length) {
    list.push({
      type: 'image',
      src: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/ellipsis-icon.png',
      css: {
        width: '44rpx',
        height: '44rpx',
        border: '1px solid #fff',
        borderRadius: '50%',
        marginLeft: '-16rpx',
      }
    })
  }

  return [
    {
      path,
      title: data.title,
      imageUrl: coverUrl
    },
    {
      path,
      title: data.title,
      board: {
        css: {
          width: imgSize.width,
          height: imgSize.height,
        },
        views: [
          {
            src: coverUrl,
            type: 'image',
            css: {
              width: '100%',
              height: '336rpx',
              objectFit: 'cover',
            },
          },
          {
            type: 'view',
            css: {
              background: 'linear-gradient(180deg, rgba(242, 243, 245, 0.00) 0%, #F2F3F5 100%)',
              height: '112rpx',
              position: 'absolute',
              left: 0,
              bottom: 0,
              width: '100%',
            },
            views: [
              {
                type: 'view',
                css: {
                  background: '#EA5E0E',
                  borderRadius: '32rpx',
                  height: '64rpx',
                  margin: '0 16rpx',
                  marginTop: '32rpx',
                  textAlign: 'center'
                },
                views: [
                  {
                    text: '参加活动',
                    type: 'text',
                    css: {
                      fontSize: '28rpx',
                      color: '#fff',
                      lineHeight: '64rpx',
                    }
                  }
                ]
              },
            ],
          },
        ],
      },
    },
    {
      path,
      title: data.title,
      board: {
        css: {
          width: imgSize.width,
          height: imgSize.height,
        },
        views: [
          {
            type: 'image',
            src: coverUrl,
            css: {
              width: '100%',
              height: '184rpx',
              borderRadius: '8rpx',
              objectFit: 'cover',
              marginBottom: '10rpx'
            }
          },
          {
            type: 'view',
            css: {
              marginBottom: '8rpx',
            },
            views: [
              {
                type: 'image',
                src: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/time-icon-1.png',
                css: {
                  width: '32rpx',
                  height: '32rpx',
                }
              },
              {
                type: 'text',
                text: `${formatToDateTime(data.startTime, 'MM月DD日 HH:mm')} ~ ${formatToDateTime(data.endTime, 'MM月DD日 HH:mm')}`,
                css: {
                  width: '85%',
                  marginLeft: '10rpx',
                  fontSize: '12px',
                  color: '#999996',
                  lineClamp: 1
                }
              }
            ]
          },
          {
            type: 'view',
            css: {
              marginBottom: '16rpx',
            },
            views: [
              {
                type: 'image',
                src: data.addressName ? 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/address-icon-1.png' : '',
                css: {
                  width: '32rpx',
                  height: '32rpx',
                }
              },
              {
                type: 'text',
                text: data.addressName,
                css: {
                  width: '85%',
                  marginLeft: '10rpx',
                  fontSize: '12px',
                  color: '#999996',
                  lineClamp: 1,
                }
              }
            ]
          },
          {
            type: 'view',
            views: list
          }
        ]
      }
    },
  ]
}

/**
 * @description 判断活动报名人数是否已满
 * @date 27/08/2024
 * @export
 * @param {number} [maxNum]
 * @param {number} [passedNum]
 * @returns {*}  {boolean}
 */
export function isActivityNumMax(maxNum?: number, passedNum?: number): boolean {
  if (!maxNum || !passedNum) return false;
  return maxNum - passedNum <= 0;
}

/**
 * @description 判断自己是否报名
 * @date 27/08/2024
 * @export
 * @param {IActivityListResData} data
 * @returns {*}  boolean
 */
export function getSelfIsSignUp(data: IActivityListResData): boolean {
  const { register } = data!;
  if (!register?.id) return false;
  if (
    register?.approvalStatus === ApprovalStatusEnum.REJECT ||
    register.approvalStatus === ApprovalStatusEnum.CANCEL
  ) {
    return false;
  }

  return true;
}

interface IToActivityRegistration {
  data: IActivityListResData
  /** 路由场景 活动详情 | 活动签到 */
  routerScene?: 'detail' | 'signIn'
  /** 分享key */
  shareKey?: string
  /** 是否验证活动报名人数 */
  isVerifyNumber?: boolean
  /** 设备编号 */
  equipmentNumber?: string
}
/**
 * @description 跳转到活动报名页面
 * @date 27/08/2024
 * @export
 * @param {{ data: IActivityListResData, shareKey?: string, isVerifyNumber?: boolean }} options
 */
export function toActivityRegistration(options: IToActivityRegistration) {
  const { data, shareKey, isVerifyNumber, routerScene, equipmentNumber } = options
  if (!data) {
    reLaunch({
      name: 'activity',
      options: {}
    })
  }
  const routerType = routerScene === 'signIn' ? reLaunch : navigateTo
  const { onlyOneTicketVo, enrollRestrict, maxNum, passedNum } = data
  const selfIsSignUp = getSelfIsSignUp(data)
  if (enrollRestrict && isActivityNumMax(maxNum, passedNum) && isVerifyNumber) {
    uni.showModal({
      title: '提示',
      content: '活动报名人数已满，不可继续报名！',
      showCancel: false,
    });
    return;
  }
  if (onlyOneTicketVo) {
    routerType({
      name: 'createActivityRegisterOrder',
      options: {
        ticketId: onlyOneTicketVo.id,
        selectedQuantity: 1,
        selfIsSignUp,
        shareKey: shareKey,
        equipmentNumber,
        routerScene
      },
    });
    return;
  }

  routerType({
    name: 'selectTicketType',
    options: {
      selfIsSignUp,
      shareKey: shareKey,
      equipmentNumber,
      routerScene
    },
  });
}